#include "UTImage.h"
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>


UTImage::UTImage(const char *fn)
{
	data = NULL;
    loaded = false;
    loaded = createTGA(fn);
}

UTImage::~UTImage()
{
    // Cleanup
    delete [] this->data;
}



bool UTImage::createTGA(const char *fn)
{
    unsigned char	TGAheader[12]	= {0,0,2,0,0,0,0,0,0,0,0,0};// Uncompressed TGA header
    unsigned char	TGAcompare[12];								// Used to compare TGA header
    unsigned char	header[6];									// First 6 useful bytes of the header
    unsigned int	bytesPerPixel;								// Holds the number of bytes per pixel used
    unsigned int	imageSize;									// Used to store the image size
    unsigned int	temp;										// Temporary variable

    FILE *file = fopen(fn, "rb");							// Open the TGA file

    // Load the file and perform checks
    if(file == NULL ||														// Does file exist?
       fread(TGAcompare,1,sizeof(TGAcompare),file) != sizeof(TGAcompare) ||	// Are there 12 bytes to read?
       memcmp(TGAheader,TGAcompare,sizeof(TGAheader)) != 0				 ||	// Is it the right format?
       fread(header,1,sizeof(header),file) != sizeof(header))				// If so then read the next 6 header bytes
    {
        if (file == NULL)									// If the file didn't exist then return
        {
            return false;
        }
        else
        {
            fclose(file);									// If something broke then close the file and return
            return false;
        }
    }

    // Determine the TGA width and height (highbyte*256+lowbyte)
    this->width  = header[1] * 256 + header[0];
    this->height = header[3] * 256 + header[2];

    // Check to make sure the targa is valid and is 24 bit or 32 bit
    if (this->width	<=0	||										// Is the width less than or equal to zero
        this->height	<=0	||										// Is the height less than or equal to zero
        (header[4] != 24 && header[4] != 32))				// Is it 24 or 32 bit?
    {
        fclose(file);										// If anything didn't check out then close the file and return
        return false;
    }

    this->bpp		= header[4];							// Grab the bits per pixel
    bytesPerPixel	= this->bpp / 8;								// Divide by 8 to get the bytes per pixel
    imageSize		= this->width * this->height * bytesPerPixel;		// Calculate the memory required for the data

    // Allocate the memory for the image data
    this->data		= new unsigned char[imageSize];

    // Make sure the data is allocated write and load it
    if(this->data == NULL ||									// Does the memory storage exist?
       fread(this->data, 1, imageSize, file) != imageSize)	// Does the image size match the memory reserved?
    {
        if(this->data != NULL)								// Was the image data loaded
            free(this->data);								// If so, then release the image data

        fclose(file);										// Close the file
        return false;
    }

    // Loop through the image data and swap the 1st and 3rd bytes (red and blue)
    for(unsigned int i = 0; i < imageSize; i += bytesPerPixel)
    {
        temp = this->data[i];
        this->data[i] = this->data[i + 2];
        this->data[i + 2] = temp;
    }

    // We are done with the file so close it
    fclose(file);

    //file name
    this->fileName = fn;

    return true;
}


UTPixel UTImage::getPixel(int x, int y)
{
    int ind = y * width * (bpp / 8) + x * (bpp / 8);
    unsigned char* pp = &data[ind];
    UTPixel px;
    px.r = pp[0];
    px.g = pp[1];
    px.b = pp[2];
    if (bpp == 32)
        px.a = pp[3];
    else
        px.a = 255;
    return px;
}

